package com.example.administrator.daggerdemo.yujian;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @项目名 Bot
 * @包名 org.auspeed.bot.util
 * @类名 DateUtils
 * @时间 2016/11/19
 * @作者 YJ
 * @描述 TODO:日期工具类
 */
public class DateUtils {
    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getDate() {
        Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        SimpleDateFormat mSmpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return mSmpleDateFormat.format(mCalendar.getTime());
    }

    /**
     * 获取该日期的之前或者之后的日期
     *
     * @param mDate
     * @param i
     * @return
     */
    public static Date getSomeday(String mDate, int i) {
        Calendar mCalendar = Calendar.getInstance();
        if (mDate != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(mDate);
                mCalendar.setTime(date);
            } catch (Exception e) {

                e.getMessage();
            }
        }
        mCalendar.add(Calendar.DAY_OF_MONTH, i);
        return mCalendar.getTime();
    }

    /**
     * 当前日期是周几
     *
     * @param mDate
     * @return 1 2 3 4 5 6 0
     */
    public static int getNowWeek(String mDate) {
        Calendar mCalendar = Calendar.getInstance();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(mDate);
            mCalendar.setTime(date);
        } catch (ParseException e) {
            e.getMessage();
        }
        return mCalendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 获取这一天所在周的时间
     *
     * @param mDate
     * @return
     */
    public static List<Date> getDaysToWeek(String mDate) {
        int week = getNowWeek(mDate);
        if (week == 1) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return dateToWeeks(sdf.parse(mDate));
            } catch (ParseException e) {
                e.getMessage();
                return dateToWeeks(new Date());
            }
        } else if (week == 0) {
            return dateToWeeks(getSomeday(mDate, -6));
        } else {
            return dateToWeeks(getSomeday(mDate, -(week - 1)));
        }
    }

    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<Date> dateToWeeks(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a - 1, fdate);
        }
        return list;
    }

    /**
     * 获取某日期前n天集合
     *
     * @param date
     * @param n
     * @return
     */
    public static List<Date> getBeforeDateToNumber(Date date, int n) {

        List<Date> mDate = new ArrayList<>();

        for (int i = n; i > 0; i--) {
            mDate.add(getBeforeDate(date, i));
        }
        mDate.add(date);

        return mDate;
    }


    /**
     * 获取某日期后n天集合
     *
     * @param date
     * @param n
     * @return
     */
    public static List<Date> getAfterDateToNumber(Date date, int n) {
        List<Date> mDate = new ArrayList<>();
        mDate.add(date);
        for (int i = 1; i < n + 1; i++) {
            mDate.add(getAfterDate(date, i));
        }
        return mDate;
    }

    /**
     * 获取某日期往前多少天的日期
     *
     * @param nowDate
     * @param beforeNum
     * @return
     */
    public static Date getBeforeDate(Date nowDate, Integer beforeNum) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(nowDate);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -beforeNum);  //设置为前beforeNum天
        return calendar.getTime();   //得到前beforeNum天的时间
    }

    /**
     * 获取某日期往前多少天的日期
     *
     * @param nowDate
     * @param beforeNum
     * @return
     */
    public static Date getAfterDate(Date nowDate, Integer beforeNum) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(nowDate);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, beforeNum);  //设置为前beforeNum天
        return calendar.getTime();   //得到前beforeNum天的时间
    }

    /**
     * 获取某日期往前多少小时的时间
     *
     * @param nowDate
     * @param beforeNum
     * @return
     */
    public static Date getBeforeHour(Date nowDate, Integer beforeNum) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(nowDate);//把当前时间赋给日历
        calendar.add(Calendar.HOUR_OF_DAY, -beforeNum);  //设置为前beforeNum小时
        return calendar.getTime();   //得到前beforeNum小时的时间
    }
}
